<template>
    <h2 align="center">优惠券修改</h2>
    <table align="center" border="1">
        <tr>
            <td>优惠券类型</td>
            <td><select v-model="state.discounttypid">
                <option value="">请选择</option>
                <option :value="item.id" v-for="item in discounttypeList">{{item.discounttypeName}}</option>
            </select></td>
        </tr>
        <tr>
            <td>优惠券标题</td>
            <td><input type="text" v-model="state.discountname"></td>
        </tr>
        <tr>
            <td>优惠颜色</td>
            <td><input type="text" v-model="state.discountcolor"></td>
        </tr>
        <tr>
            <td>门店</td>
            <td><select v-model="state.storeid">
                <option value="">请选择</option>
                <option :value="item.id" v-for="item in storelist">{{item.storeName}}</option>
            </select></td>
        </tr>
        <tr>
            <td>限领人数</td>
            <td><input type="text" v-model="state.xnum"></td>
        </tr>
        <tr>
            <td>使用条件</td>
            <td><input type="text" v-model="state.pmoney"></td>
        </tr>
        <tr>
            <td>减免金额</td>
            <td><input type="text" v-model="state.jmoney"></td>
        </tr>
        <tr>
            <td>折扣</td>
            <td><input type="text" v-model="state.zhenko"></td>
        </tr>
        <tr>
            <td>开始时间</td>
            <td><input type="date" v-model="state.stime"></td>
        </tr>
        <tr>
            <td>结束时间</td>
            <td><input type="date" v-model="state.etime"></td>
        </tr>
        <tr>
            <td>优惠券数量</td>
            <td><input type="text" v-model="state.ycount"></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="button" value="编辑" @click="youupd"></td>
        </tr>
    </table>
</template>

<script setup lang="ts">
// v-if="state.discounttypid==2"
import {reactive,toRefs,onMounted} from 'vue';
import axios from 'axios';
import {useRouter,useRoute} from 'vue-router';
const router = useRouter()
const route = useRoute()
const data = reactive({
    state:{
 "discounttypid": '',
  "discountname":'',
  "discountcolor":'',
  "storeid":'',
  "xnum": '',
  "pmoney": '',
  "jmoney": '',
  "zhenko": '',
  "stime": '',
  "etime":'',
  "isDeleted": false,
  "createTime": "2024-08-26T03:12:58.825Z",
  "ycount":'',
  "state":1
    },
    discounttypeList:[],
    storelist:[]
})

const {state,discounttypeList,storelist} = toRefs(data)

onMounted(()=>{
 GetDatatype();
 GetMendian();
 fantian();
})

const fantian=()=>{
    axios({
        url:'/api/Discount/Fantian',
        method:'get',
        params:{
            id:route.query.id
        }
    })
    .then(res=>{
        console.log(res);
        state.value=res.data.data;
        state.value.stime=state.value.stime.slice(0,10);
        state.value.etime=state.value.etime.slice(0,10);
    })
    .catch(err=>{
        console.log(err);
    })
}



//获取优惠券类型
const GetDatatype=()=>{
    axios({
        url:'/api/Discount/ShowDis',
        method:'get'
    })
    .then(res=>{
        console.log(res);
        discounttypeList.value=res.data;
    })
    .catch(err=>{
        console.log(err);
    })
}

//获取门店
const GetMendian=()=>{
    axios({
        url:'/api/Store/XianShi',
        method:'get'
    })
    .then(res=>{
        console.log(res);
        storelist.value=res.data;
    })
    .catch(err=>{
        console.log(err);
    })
}
//添加
const youupd=()=>{
    if(state.value.discounttypid==''){
        alert('优惠券类型不能为空')
        return;
    }
    if(state.value.discountname==''){
        alert('优惠券标题不能为空')
        return;
    }
    if(state.value.discountcolor==''){
        alert('优惠券颜色不能为空')
        return;
    }
    if(state.value.storeid==''){
        alert('门店不能为空')
        return;
    }
    if(state.value.xnum==''){
        alert('限领人数不能为空')
        return;
    }
    if(state.value.pmoney==''){
        alert('使用条件不能为空')
        return;
    }
    if(state.value.jmoney==''){
        alert('减免金额不能为空')
        return
    }
    
    if(state.value.zhenko==''){
        alert('折扣不能为空')
        return;
    }
    
    if(state.value.stime==''){
        alert('开始时间不能为空')
        return;
    }
    if(state.value.etime==''){
        alert('结束时间不能为空')
        return
    }
    if(state.value.ycount==""){
        alert('优惠券数量不能为空')
        return;
    }
    axios({
        url:'/api/Discount/DisCountUpd',
        method:'put',
        data:state.value
    })
    .then(res=>{
        console.log(res);
        if(res.data.data>0){
            alert('修改成功')
            router.push('/Memshow')
        }
        else{
            alert('修改失败')
        }
    })
    .catch(err=>{
        console.log(err);
    })
}
</script>